import { ref } from 'vue'
import type PageModal from '@/components/page-modal/page-modal.vue'

type CallbackFnType = (data: any) => void
type CallbackFnType2 = () => void

function usePageModal(newCallback?: CallbackFnType2, callback?: CallbackFnType) {
  const modalRef = ref<InstanceType<typeof PageModal>>()

  function showModal() {
    modalRef.value?.setVisible()

    if (newCallback) {
      newCallback()
    }
  }

  function handleEdit(itemData: any) {
    modalRef.value?.setVisible(itemData)

    if (callback) {
      callback(itemData)
    }
  }

  return {
    modalRef,
    showModal,
    handleEdit
  }
}

export default usePageModal
